package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.system.domain.business.bo.PoctQualityRulesItemBo;
import com.rzdata.poct.system.domain.business.po.PoctQualityRulesItem;
import com.rzdata.poct.system.domain.business.vo.PoctQualityRulesItemVo;
import com.rzdata.poct.system.mapper.PoctQualityRulesItemMapper;
import com.rzdata.poct.system.service.IPoctQualityRulesItemService;
import org.springframework.stereotype.Service;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 规则附Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-18
 */
@Service
public class PoctQualityRulesItemServiceImpl extends ServicePlusImpl<PoctQualityRulesItemMapper, PoctQualityRulesItem, PoctQualityRulesItemVo> implements IPoctQualityRulesItemService {

    @Override
    public PoctQualityRulesItemVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctQualityRulesItemVo> queryPageList(PoctQualityRulesItemBo bo) {
        PagePlus<PoctQualityRulesItem, PoctQualityRulesItemVo> result =
            pageVo(PageUtils.buildPagePlusNew(Long.valueOf(bo.getPageNum()), Long.valueOf(bo.getPageSize())), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<PoctQualityRulesItemVo> queryList(PoctQualityRulesItemBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctQualityRulesItem> buildQueryWrapper(PoctQualityRulesItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctQualityRulesItem> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getRuleId() != null, PoctQualityRulesItem::getRuleId, bo.getRuleId());
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), PoctQualityRulesItem::getProjectName, bo.getProjectName());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyOne()), PoctQualityRulesItem::getKeyOne, bo.getKeyOne());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyTwo()), PoctQualityRulesItem::getKeyTwo, bo.getKeyTwo());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyThree()), PoctQualityRulesItem::getKeyThree, bo.getKeyThree());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyFour()), PoctQualityRulesItem::getKeyFour, bo.getKeyFour());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyFive()), PoctQualityRulesItem::getKeyFive, bo.getKeyFive());
        lqw.eq(StringUtils.isNotBlank(bo.getKeySix()), PoctQualityRulesItem::getKeySix, bo.getKeySix());
        lqw.eq(bo.getQualityNum() != null, PoctQualityRulesItem::getQualityNum, bo.getQualityNum());
        lqw.eq(bo.getStartTime() != null, PoctQualityRulesItem::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, PoctQualityRulesItem::getEndTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getLevelNoe()), PoctQualityRulesItem::getLevelNoe, bo.getLevelNoe());
        lqw.eq(StringUtils.isNotBlank(bo.getLevelTwo()), PoctQualityRulesItem::getLevelTwo, bo.getLevelTwo());
        lqw.eq(StringUtils.isNotBlank(bo.getLevelThree()), PoctQualityRulesItem::getLevelThree, bo.getLevelThree());
        lqw.eq(StringUtils.isNotBlank(bo.getLevelFour()), PoctQualityRulesItem::getLevelFour, bo.getLevelFour());
        lqw.eq(StringUtils.isNotBlank(bo.getLevelFive()), PoctQualityRulesItem::getLevelFive, bo.getLevelFive());
        lqw.eq(StringUtils.isNotBlank(bo.getLevelSix()), PoctQualityRulesItem::getLevelSix, bo.getLevelSix());
        lqw.eq(StringUtils.isNotBlank(bo.getSwitchOne()), PoctQualityRulesItem::getSwitchOne, bo.getSwitchOne());
        lqw.eq(StringUtils.isNotBlank(bo.getSwitchTwo()), PoctQualityRulesItem::getSwitchTwo, bo.getSwitchTwo());
        lqw.eq(StringUtils.isNotBlank(bo.getSwitchThree()), PoctQualityRulesItem::getSwitchThree, bo.getSwitchThree());
        lqw.eq(StringUtils.isNotBlank(bo.getPointOne()), PoctQualityRulesItem::getPointOne, bo.getPointOne());
        lqw.eq(StringUtils.isNotBlank(bo.getPointTwo()), PoctQualityRulesItem::getPointTwo, bo.getPointTwo());
        lqw.eq(StringUtils.isNotBlank(bo.getPointThree()), PoctQualityRulesItem::getPointThree, bo.getPointThree());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctQualityRulesItemBo bo) {
        PoctQualityRulesItem add = BeanUtil.toBean(bo, PoctQualityRulesItem.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByBo(PoctQualityRulesItemBo bo) {
        PoctQualityRulesItem update = BeanUtil.toBean(bo, PoctQualityRulesItem.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctQualityRulesItem entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
